package com.lagou.edu.service.impl;

import com.lagou.edu.dao.LagouTokenDao;
import com.lagou.edu.dao.LagouUserDao;
import com.lagou.edu.entity.LagouToken;
import com.lagou.edu.entity.LagouUser;
import com.lagou.edu.service.UserDubboService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.util.ObjectUtils;

import java.util.Optional;

/**
 * @author zhangjinshan
 * @version 1.0
 * @description
 * @date 2021/9/1 上午9:29
 */
@Service
public class UserDubboServiceImpl implements UserDubboService {

    @Autowired
    private LagouUserDao lagouUserDao;

    @Autowired
    private LagouTokenDao lagouTokenDao;

    @Override
    public boolean isRegister(String email) {
        LagouUser condition = LagouUser.builder()
                .email(email)
                .build();
        Example<LagouUser> example = Example.of(condition);
        Optional<LagouUser> optional = lagouUserDao.findOne(example);
        if (ObjectUtils.isEmpty(optional)) {
            return false;
        }
        return true;
    }

    @Override
    public String info(String token) {
        LagouToken condition = new LagouToken();
        condition.setToken(token);
        Example<LagouToken> example = Example.of(condition);
        Optional<LagouToken> optional = lagouTokenDao.findOne(example);
        if (ObjectUtils.isEmpty(optional)) {
            return null;
        }
        return optional.get().getEmail();
    }
}
